<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>新增商品</title>
<!--公共样式开始-->
<link rel="stylesheet" type="text/css" href="{$NOW_THEME_URL}/Assets/Css/reset.css">
<link rel="stylesheet" type="text/css" href="{$NOW_THEME_URL}/Assets/Css/common.css">
<link rel="stylesheet" type="text/css" href="{$NOW_THEME_URL}/Assets/Plugins/FontAwesome/css/font-awesome.min.css">
<!--公共样式结束-->
<link rel="stylesheet" type="text/css" href="{$NOW_THEME_URL}/Assets/Css/module_form.css">
<link rel="stylesheet" type="text/css" href="{$NOW_THEME_URL}/Assets/Css/page_memberAdd.css">
<script src="{$NOW_THEME_URL}/Assets/Js/jquery.min.js"></script>
<script src="{$NOW_THEME_URL}/Assets/Js/common.js"></script>
<script src="{$NOW_THEME_URL}/Assets/Plugins/Datepicker/wdatePicker.js"></script>
</head>

<body>
	<div class="page_header">
		<div class="tabbar">
			<ul class="tab clearfix" tabname="tab1">
				<li class="tabhover" show="tab1_0">基本信息</li>
				<li show="tab1_1">计费规则</li>
				<li show="tab1_2">促销规则</li>
				<li show="tab1_3">更多设置</li>
			</ul>
		</div>
	</div>
	<div class="page_wraper">
		<div id="tab1_0" fortab="tab1">
			<input type="hidden" id="solevar" name="solevar" value="{:create_id()}">
			<table cellpadding="0" cellspacing="0" class="module_form" width="600">
				<tr>
					<td class="ele_title"><label for="cardnum">商品编号：<em>*</em></label></td>
					<td class="ele_content">
						<input name="pno" value="{$detail.pno}" type="text" id="pno" class="form_ele_text width_200"></td>
					<td class="ele_title"><label for="realname">名称：<em>*</em></label></td>
					<td class="ele_content">
						<input name="title" value="{$detail.title}" type="text" id="title" class="form_ele_text width_200"></td>
				</tr>
				<tr>
					<td colspan="2">&nbsp;</td>
					<td class="ele_title"><label for="realname">商品简称：<em>*</em></label></td>
					<td class="ele_content">
						<input name="title" value="{$detail.ctitle}" type="text" id="ctitle" class="form_ele_text width_200"></td>
				</tr>
				<tr>
					<td class="ele_title"><label for="leibie">所属类别：<em>*</em></label></td>
					<td class="ele_content">
						<select class="width_200" name="cateid" id="cateid">
							<option value="">请选择</option>
							<volist name="column" id="v">
								<option value="{$v.id}">{$v.title}</option>
							</volist>
						</select>
					</td>
					<td class="ele_title"><label for="pinpai">所属品牌：</label></td>
					<td class="ele_content">
						<select class="width_200" name="brandid" id="brandid">
							<option value="">请选择</option>
							<volist name="brands" id="v">
								<option value="{$v.id}" 
								<eq name="v.id" value="$detail.brandid">selected</eq>>{$v.title}</option>
							</volist>
						</select>
					</td>
				</tr>
				<tr>
					<td class="ele_title"><label for="level">计费类型：<em>*</em></label></td>
					<td class="ele_content">
						<select class="width_200" name="typeid" id="typeid">
							<option value="">请选择</option>
							<volist name="billing" id="v">
								<option value="{$key}"
								<eq name="key" value="$detail.typeid">selected</eq>>{$v.title}</option>
							</volist>
						</select>
					</td>
					<td class="ele_title"><label for="kucun">计费单位：<em>*</em></label></td>
					<td class="ele_content">
						<select class="width_200" name="unit" id="unit">
							<volist name="unitArr" id="vo">
								<neq name="key" value="title">
									<option value="{$key}" <eq name="key" value="$detail.unit">selected</eq>>{$vo}</option>
								</neq>
							</volist>
						</select>
					</td>
				</tr>
				<tr>
					<td class="ele_title"><label for="cbprice">成本价：<em>*</em></label></td>
					<td class="ele_content">
						<input name="mprice" value="{$detail.mprice}" type="text" id="mprice" class="form_ele_text width_200"></td>
					<td class="ele_title"><label for="lsprice">零售价：<em>*</em></label></td>
					<td class="ele_content">
						<input name="sprice" value="{$detail.sprice}" type="text" id="sprice" class="form_ele_text width_200"></td>
				</tr>
				<tr>
					<td class="ele_title"><label for="kucun">库存：</label></td>
					<td class="ele_content" colspan="2">
						<input name="stock" value="{$detail.stock}" type="text" id="stock" class="form_ele_text width_200"></td>
				</tr>
			</table>
		</div>
		<div id="tab1_1" fortab="tab1" class="clearfix" style="display:none;">
			<table cellpadding="0" cellspacing="0" class="module_form xztable" width="600">
				<thead>
					<tr>
						<th class="center">数量</th>
						<th class="center">单位</th>
						<th class="center">成本价</th>
						<th class="center">零售价</th>
						<th class="center xinzeng" id="xinzeng"><i class="icon-plus"></i></th>
					</tr>
				</thead>
				<tbody>
					<empty name="attr">
						{$empty}
						
					<else />
						<volist name="attr" id="v" key="j" empty="$empty">
							<tr>
								<td class="center">
									<input type="text" name="cnum[]" value="{$v.cnum}" class="form_ele_text width_150 cnum"></td>
								<td class="center">
									<select class="width_150 unitHtml" name="unit[]" id="unitHtml">
										<volist name="unitArr" id="vo">
											<option value="{$key}" <eq name="key" value="$detail.unit">selected</eq>>{$vo}</option>
										</volist>
									</select>
								</td>
								<td class="center">
									<input name="mprice[]" value="{$v.mprice}" type="text" id="" class="form_ele_text width_150 mprice"></td>
								<td class="center">
									<input name="sprice[]" value="{$v.sprice}" type="text" id="" class="form_ele_text width_150 sprice"></td>
								<td class="center deal">
									<neq name="j" value="1"><a class="bg_red">删除</a><else />&nbsp;</neq>
								</td>
							</tr>
						</volist>
					</empty>
				</tbody>
			</table>
		</div>
		<div id="tab1_2" fortab="tab1" class="clearfix" style="display:none;">
			<table cellpadding="0" cellspacing="0" class="module_form" width="600">
				<tr class="cuxiao">
					<td colspan="1">
						<input name="isspecial" id="isspecial" class="checkbox" type="checkbox" value="1"
						<eq name="detail.isspecial" value="1">checked</eq>>是否特价</td>
					<td colspan="3" class="left">
						<span><input name="special" value="{$detail.special}" type="text" id="special" class="form_ele_text width_200"><em>元</em></span>
					</td>
				</tr>
				<tr class="cuxiao">
					<td colspan="1"><input name="isintegral" id="isintegral" class="checkbox" type="checkbox" value="1"
					<eq name="detail.isintegral" value="1">checked</eq>>是否赠送积分</td>
					<td colspan="3" class="left">
						<span><input name="integral" value="{$detail.integral}" type="text" id="integral" class="form_ele_text width_200"><em>分</em></span>
					</td>
				</tr>
				<tr class="cuxiao">
					<td colspan="1"><input name="isdiscount" id="isdiscount" class="checkbox" type="checkbox" value="1"
					<eq name="detail.isdiscount" value="1">checked</eq>>是否打折</td>
					<td colspan="3" class="left">
						<span><input name="discount" value="{$detail.discount}" type="text" id="discount" class="form_ele_text width_200"><em>折</em></span>
					</td>
				</tr>
			</table>
		</div>
		<div id="tab1_3" fortab="tab1" class="clearfix" style="display:none;">
			<table cellpadding="0" cellspacing="0" class="module_form" width="600">
				<tr>
					<td class="ele_title"><label for="gjz">关键字：</label></td>
					<td class="ele_content"><input name="keywords" value="{$detail.keywords}" type="text" id="keywords" class="form_ele_text width_200"></td>
					<td class="ele_title"><label for="jianjie">简介：</label></td>
					<td class="ele_content"><input name="description" value="{$detail.description}" type="text" id="description" class="form_ele_text width_200"></td>
				</tr>
				<!--  <tr>
					<td class="ele_title"><label for="stime">上架时间：</label></td>
					<td class="ele_content"><input name="" type="text" id="d411 stime" class="form_ele_text width_200 Wdate" onfocus="WdatePicker({dateFmt:'yyyy-MM-dd',maxDate:'%y-%M-%d'})"></td>
					<td class="ele_title"><label for="xtime">下架时间：</label></td>
					<td class="ele_content"><input name="" type="text" id="d411 xtime" class="form_ele_text width_200 Wdate" onfocus="WdatePicker({dateFmt:'yyyy-MM-dd',maxDate:'%y-%M-%d'})"></td>
				</tr>-->
				<tr>
					<td class="ele_title"><label for="gjz">排序号：</label></td>
					<td class="ele_content"><input name="sort" value="{$detail.sort}" type="text" id="sort" class="form_ele_text width_200"></td>
					<td class="ele_title"><label for="jianjie">缩略图（4:3）：</label></td>
					<td class="ele_content"><a href="" class="upload"><i class="icon-picture"></i>上传缩略图（4:3）</a></td>
				</tr>
				<tr>
					<td class="ele_title"><label for="remark">详细介绍：</label></td>
					<td colspan="3" class="ele_content">
						<textarea class="width_90" name="content" id="content">{$detail.content}</textarea>
					</td>
				</tr>
			</table>
		</div>
	</div>
	<div class="page_footer pdtd10">
		<input type="button" name="submit" value="提交数据"  class="form_ele_submit"/>
		<input type="reset" name="reset" value="清空数据"  class="form_ele_reset"/>
	</div>
</body>
<script type="text/javascript">
	//弹出窗口全局变量
	var api  = frameElement.api, W = api.opener;

	$(function(){
		//滑动门
		$('.tabbar li').click(function(){
			var tabname = $('.tabbar>ul').attr('tabname');
			$('.tabbar li').removeClass('tabhover');
			$(this).addClass('tabhover');
			
			$('div[fortab="'+tabname+'"]').hide();
			
			var showContent = $('#'+$(this).attr('show')); //当前要显示的内容对象
			showContent.show();
		});
		
		//动态改变计费单位
		$('#typeid').change(function(){
			var typeid = $('#typeid').val();
			if(typeid){
				$.get("{:U('Product/changedType')}",{typeid:typeid},function(data){
					$('#unit').html(data);
				});
			}
		});
		
		//生成计费规则
		$('#sprice').change(function(){
			change();
		});
		$('#unit').change(function(){
			change();
		});
		$('#mprice').change(function(){
			change();
		});
		
		//表格隔行换色
		$('.module_form tr:odd').addClass('odd');
		$('.module_form tr:even').addClass('even');
		
		//表单操作
		//新增
		$('#xinzeng').click(function(){
			
			var typeid = $('#typeid').val();
			var unit  = $('#unit').val();
			
			if(typeid&&unit){
				$.get("{:U('Product/changedType')}",{typeid:typeid,unit:unit},function(data){
					var content = '<tr><td class="center"><input name="cnum" type="text" id="cnum" class="form_ele_text width_150 cnum"></td>\
								  <td class="center"><select class="width_150 unitHtml" name="" id="unitHtml" disabled>'+data+'</select></td>\
								  <td class="center"><input name="mprice[]" type="text" class="form_ele_text width_150 mprice"></td>\
								  <td class="center"><input name="sprice[]" type="text" class="form_ele_text width_150 sprice"></td>\
								  <td class="center deal"><a class="bg_red">删除</a></td></tr>';
					//表格隔行换色
					$('.xztable>tbody').append(content);
					$('.module_form tr:odd').addClass('odd');
					$('.module_form tr:even').addClass('even');
				});
			}else{
				windowApi.lhgdialog.alert('请选择计费类型与计费单位！',function(){return;});
			}
		});
		
		//删除计费规则
		$('.bg_red').live('click',function(data){
			$(this).parents('tr').remove();
		})
		
		//自动计算成本价与零售价
		$('#cnum').live('change',function(data){
			var cnum   = $(this).val();
			var mprice = cnum*($('#mprice').val());
			var sprice = cnum*($('#sprice').val());
			
			$(this).parent('td').children('input #mprice').val(mprice);
			$(this).parent('td').children('input #sprice').val(sprice);
		})
		
		//重置
		$('.form_ele_reset').on('click',function(){
			$('#productAdd_form').get(0).reset();
		});
		//提交
		$('.form_ele_submit').on('click',function(){
			
			var tipsObj = windowApi.lhgdialog.tips('正在提交数据...',2,etIcon.loading);
			
			var gmprice = {};
			var gsprice = {};
			var gcnum   = {};
			$(".cnum").each(function(i){gcnum[i] = $(this).val();});
			$(".mprice").each(function(n){gmprice[n] = $(this).val();});
			$(".sprice").each(function(m){gsprice[m] = $(this).val();});
			
			//通过Ajax提交表单
			var postData = {};
			postData.gcnum      = gcnum;
			postData.gmprice    = gmprice;
			postData.gsprice    = gsprice;
			postData.solevar    = $('#solevar').val();//唯一标识
			postData.cateid     = $('#cateid').val(); //获取商品类别id
			postData.pno        = $('#pno').val(); //获取商品编号
			postData.brandid    = $('#brandid').val(); //获取商品品牌id
			postData.title      = $('#title').val(); //获取商品名称
			postData.ctitle     = $('#ctitle').val(); //获取商品简称
			postData.keywords   = $('#keywords').val(); //获取商品关键字
			postData.content    = $('#content').val(); //获取商品详情
			postData.sort       = $('#sort').val(); //获取排序号
			postData.mprice     = $('#mprice').val(); //获取成本价
			postData.sprice     = $('#sprice').val(); //获取销售价
			postData.stock      = $('#stock').val(); //获取商品库存
			postData.typeid     = $('#typeid').val(); //获取计费类型
			postData.cnum       = $('#cnum').val(); //获取计费数量
			postData.unit       = $('#unit').val(); //获取计费单位
			postData.attr       = $('#attr').val(); //获取计费规则
			postData.special    = $('#special').val(); //获取特价价格
			postData.discount   = $('#discount').val(); //获取折扣
			postData.integral   = $('#integral').val(); //获取积分
			postData.description= $('#description').val(); //获取商品简介
			
			if($("#isspecial").attr("checked")=="checked")
			postData.isspecial  = $('#isspecial').val(); //获取是否特价
			if($("#isintegral").attr("checked")=="checked")
			postData.isintegral = $('#isintegral').val(); //获取是否赠送积分
			if($("#isdiscount").attr("checked")=="checked")
			postData.isdiscount = $('#isdiscount').val(); //获取是否折扣
			
			
			$.post("{:U('Product/add')}", postData ,
				function(data){
					tipsObj.close();
					windowApi.lhgdialog.tips('数据提交成功！',1,etIcon.success,function(){
						
						if(data.status==1){
							var obj = windowApi.lhgdialog.list['Product_index'];
							if(obj){
								obj.reload(obj);
							}else{
								var href = '{:U("Product/index")}';
								var width = '1100px';
								var height = '500px';
								var title = '商品列表';
								openDialog('Product_index',href,width,height,title);
							}
						}else{
							windowApi.lhgdialog.alert(data.info,function(){return;});
						}
						frameApi.close(); //关闭当前窗口
					});
				}, "json"
			);
		});
		
	});
	
	//生成默认的计费规则
	function change(){
		var typeid = $('#typeid').val();
		var unit  = $('#unit').val();
		var mprice = $('#mprice').val();
		var sprice = $('#sprice').val();
		
		$.get("{:U('Product/changedType')}",{typeid:typeid,unit:unit},function(reg){
			$(".unitHtml").each(function(){
				$(this).html(reg);
			});
		})
		$("input[name='cnum[]']").val("1");
		$("input[name='mprice[]']").val(mprice);
		$("input[name='sprice[]']").val(sprice);
		
	}
</script>
</html>
